package com.mysteel.cyx.demo.face.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysteel.cyx.demo.baomidou.entity.Department;
import com.mysteel.cyx.demo.baomidou.mapper.DepartmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author：cyx
 * @Date：2024/5/8 9:08
 */
@RestController
public class TransactionalAndLockTest {

    @Autowired
    private CommonService commonService;

    @GetMapping("/test")
    public String transactionalAndLock() {
        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10; j++) {
                    commonService.test();
                }
            }).start();
        }
        return "test";
    }

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 测试树形结构
     *
     * @return
     */
    @GetMapping("/testTree")
    public List<Tree<String>> testTree() {
        //1.获取所有资料分类
        List<Department> dataList = departmentMapper.selectList(Wrappers.lambdaQuery(Department.class));
        return commonService.testTree(dataList, "");
    }

    /**
     * 根据某个父类ID测试树形结构
     * 这个是不含自己层级的数据
     *
     * @return
     */
    @GetMapping("/testTreeByDepartmentId")
    public List<Tree<String>> testTree(@RequestParam("departmentId") Long departmentId) {
        //1.获取所有资料分类
        List<Department> dataList = departmentMapper.getNodeTreeByDepartmentId(departmentId);
        return commonService.testTree(dataList, departmentId.toString());
    }

    /**
     * 根据某个父类ID测试树形结构
     * 这个是含自己层级的数据,需要先把自己层级的parentId查询出来
     *
     * @return
     */
    @GetMapping("/testTreeByDepartmentId1")
    public List<Tree<String>> testTree1(@RequestParam("departmentId") Long departmentId) {
        Department department = departmentMapper.selectById(departmentId);
        //1.获取所有资料分类
        List<Department> dataList = departmentMapper.getNodeTreeByDepartmentId1(departmentId);
        return commonService.testTree(dataList, department.getParentId() == null ? "" : department.getParentId().toString());
    }


    /**
     * 根据某个父类ID测试树形结构
     * 这个是含自己层级的数据,需要先把自己层级的parentId查询出来
     *
     * @return
     */
    @GetMapping("/testTreeByDepartmentId2")
    public List<Tree<String>> testTree2(@RequestParam("departmentId") Long departmentId) {
        Department department = departmentMapper.selectById(departmentId);
        //1.获取所有资料分类
        List<Department> dataList = departmentMapper.getNodeTreeByDepartmentId2(departmentId);
        return commonService.testTree(dataList, department.getParentId() == null ? "" : department.getParentId().toString());
    }
}
